package oop.controller.action.section;

import java.sql.SQLException;

import oop.controller.action.DefaultAction;
import oop.data.Test;
import oop.db.dao.SectionDAO;
import oop.db.dao.TestDAO;
import oop.db.dao.TestVersionDAO;

public class DeleteAction extends DefaultAction {

	@Override
	public void performImpl() throws Exception {
		long sectionId = getParams().getLong("sd_id");
		long testId = getParams().getLong("sd_testid");
		Test test = TestDAO.fetchById(testId);

		try {
			TestVersionDAO.dropByTest(test.getId());
		} catch (SQLException e) {
			if (e.getErrorCode() == 1451) {
				error("Không thể sửa đề thi đang sử dụng");
				return;
			} else {
				throw e;
			}
		}

		SectionDAO.drop(sectionId);
		SectionDAO.normalize(testId);

		setNextAction("test.view&tv_id=" + testId);
	}

}
